﻿using MelonLoader;
using MOD_TaiYangQiu.common;
using MOD_TaiYangQiu.model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace MOD_TaiYangQiu.config
{
    /// <summary>
    /// 初始化配置
    /// </summary>
    public static class InitConfig
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            LogUtil.Log("太阳球MOD", "配置数据");
            FileUtil fileUtil = new FileUtil();
            string gamePath = System.Environment.CurrentDirectory + Path.DirectorySeparatorChar + "config";
            //配置文件
            string filePath = gamePath + Path.DirectorySeparatorChar + "BaseConfig.json";
            string oldPath = gamePath + Path.DirectorySeparatorChar + "BaseConfig" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".json";
            byte[] dataByte = Properties.Resources.BaseConfig;
            //说明文件
            string txtPath = gamePath + Path.DirectorySeparatorChar + "readme.md";
            byte[] txtByte = Properties.Resources.readme;
            //MelonLogger.Msg(ConsoleColor.Blue, gamePath);
            //string baseConfig = Encoding.UTF8.GetString(dataByte);
            //判断文件夹是否存在
            if (!Directory.Exists(gamePath))
            {
                Directory.CreateDirectory(gamePath);
                fileUtil.BytesFile(dataByte, filePath);
                fileUtil.BytesFile(txtByte, txtPath);
            }
            else
            {
                //判断文件是否存在
                if (!File.Exists(filePath))
                {
                    fileUtil.BytesFile(dataByte, filePath);
                    fileUtil.BytesFile(txtByte, txtPath);
                }
                else
                {
                    //比较版本是否一致,不一致则覆盖
                    string json = File.ReadAllText(filePath, Encoding.UTF8);
                    JObject jo = JObject.Parse(json);
                    string version = jo["version"].ToString();
                    if (!BaseConfig.jsonVersion.Equals(version))
                    {
                        fileUtil.ReNameFile(filePath, oldPath);
                        fileUtil.BytesFile(dataByte, filePath);
                        fileUtil.BytesFile(txtByte, txtPath);
                    }
                }

            }
            //初始化道具
            InitSetting(filePath);
        }
        /// <summary>
        /// 配置信息
        /// </summary>
        public static void InitSetting(string filePath)
        {
            string json = File.ReadAllText(filePath, Encoding.UTF8);
            JObject jo = JObject.Parse(json);
            BaseConfig.isLowGrade = Convert.ToBoolean(jo["isLowGrade"]);
            BaseConfig.isSendWelfare = Convert.ToBoolean(jo["isSendWelfare"]);
            BaseConfig.isStudyMartial = Convert.ToBoolean(jo["isStudyMartial"]);
            BaseConfig.daoHunId = Convert.ToInt32(jo["daoHunId"]);
            BaseConfig.storageIndex = Convert.ToInt32(jo["storageIndex"]);
            BaseConfig.probability = Convert.ToInt32(jo["probability"]);
            string huaShenZhiQi = jo["huaShenZhiQi"].ToString();
            BaseConfig.huaShenZhiQi = JsonConvert.DeserializeObject<HuaShenZhiQi>(huaShenZhiQi);
            string aptitudeData = jo["aptitudeData"].ToString();
            BaseConfig.aptitudeData = JsonConvert.DeserializeObject<List<AptitudeData>>(aptitudeData);
            string sendType = jo["sendType"].ToString();
            if (true)
            {
                JObject sendTypeData = JObject.Parse(sendType);
                BaseConfig.isWife = Convert.ToBoolean(sendTypeData["isWife"]);
                BaseConfig.isCouple = Convert.ToBoolean(sendTypeData["isCouple"]);
                BaseConfig.isTeacher = Convert.ToBoolean(sendTypeData["isTeacher"]);
                BaseConfig.isStudent = Convert.ToBoolean(sendTypeData["isStudent"]);
                BaseConfig.isParent = Convert.ToBoolean(sendTypeData["isParent"]);
                BaseConfig.isChildren = Convert.ToBoolean(sendTypeData["isChildren"]);
                BaseConfig.isBrother = Convert.ToBoolean(sendTypeData["isBrother"]);
                string schoolConfig = sendTypeData["school"].ToString();
                JObject schoolConfigData = JObject.Parse(schoolConfig);
                BaseConfig.school.IsSchool = Convert.ToBoolean(schoolConfigData["isSchool"]);
                BaseConfig.school.IsPointSchool = Convert.ToBoolean(schoolConfigData["isPointSchool"]);
                string daZhangLao = schoolConfigData["daZhangLao"].ToString();
                GetAreaConfig("daZhangLao", daZhangLao);
                string zhangLao = schoolConfigData["zhangLao"].ToString();
                GetAreaConfig("zhangLao", zhangLao);
                string zhenChuan = schoolConfigData["zhenChuan"].ToString();
                GetAreaConfig("zhenChuan", zhenChuan);
                string neiMen = schoolConfigData["neiMen"].ToString();
                GetAreaConfig("neiMen", neiMen);
                string waiMen = schoolConfigData["waiMen"].ToString();
                GetAreaConfig("waiMen", waiMen);


            }
            string lockLuck = jo["lockLuck"].ToString();
            if (true)
            {
                List<int> list = new List<int>();
                JArray itemDataList = JArray.Parse(lockLuck);
                for (int i = 0; i < itemDataList.Count; i++)
                {
                    list.Add(Convert.ToInt32(itemDataList[i]));
                }
                BaseConfig.lockLuck = list;
            }
            string sendWelfareMonth = jo["sendWelfareMonth"].ToString();
            if (true)
            {
                List<int> list = new List<int>();
                JArray itemDataList = JArray.Parse(sendWelfareMonth);
                for (int i = 0; i < itemDataList.Count; i++)
                {
                    list.Add(Convert.ToInt32(itemDataList[i]));
                }
                BaseConfig.sendWelfareMonth = list;
            }
            //福利
            string welfareConfig = jo["welfareConfig"].ToString();
            JObject welfareData = JObject.Parse(welfareConfig);
            AddWelfares("lianQi_Welfare", welfareData);
            AddWelfares("zhuJi_Welfare", welfareData);
            AddWelfares("jieJing_Welfare", welfareData);
            AddWelfares("jinDan_Welfare", welfareData);
            AddWelfares("juLing_Welfare", welfareData);
            AddWelfares("yuanYing_Welfare", welfareData);
            AddWelfares("huaShen_Welfare", welfareData);
            AddWelfares("wuDao_Welfare", welfareData);
            AddWelfares("yuHua_Welfare", welfareData);
            AddWelfares("dengXian_Welfare", welfareData);
            //突破材料
            string itemConfig = jo["itemConfig"].ToString();
            JObject items = JObject.Parse(itemConfig);
            AddItems("lianQi_Q_Yes", items);
            AddItems("lianQi_Z_Yes", items);
            AddItems("lianQi_H_Yes", items);
            AddItems("zhuJi_Q_Yes", items);
            AddItems("zhuJi_Z_Yes", items);
            AddItems("zhuJi_H_Yes", items);
            AddItems("jieJing_Q_Yes", items);
            AddItems("jieJing_Z_Yes", items);
            AddItems("jieJing_H_Yes", items);
            AddItems("jinDan_Q_Yes", items);
            AddItems("jinDan_Z_Yes", items);
            AddItems("jinDan_H_Yes", items);
            AddItems("juLing_Q_Yes", items);
            AddItems("juLing_Z_Yes", items);
            AddItems("juLing_H_Yes", items);
            AddItems("yuanYing_Q_Yes", items);
            AddItems("yuanYing_Z_Yes", items);
            AddItems("yuanYing_H_Yes", items);
            AddItems("huaShen_Q_Yes", items);
            AddItems("huaShen_Z_Yes", items);
            AddItems("huaShen_H_Yes", items);
            AddItems("wuDao_Q_Yes", items);
            AddItems("wuDao_Z_Yes", items);
            AddItems("wuDao_H_Yes", items);
            AddItems("yuHua_Q_Yes", items);
            AddItems("yuHua_Z_Yes", items);
            AddItems("yuHua_H_Yes", items);
            AddItems("dengXian_Q_Yes", items);
            AddItems("dengXian_Z_Yes", items);
            AddItems("dengXian_H_Yes", items);
        }
        /// <summary>
        /// 添加道具
        /// </summary>
        /// <param name="type"></param>
        /// <param name="data"></param>
        public static void AddItems(string type, JObject data)
        {
            string itemData = data[type].ToString();
            JArray itemDataList = JArray.Parse(itemData);
            for (int i = 0; i < itemDataList.Count; i++)
            {
                ItemModel itemModel = new ItemModel();
                itemModel.Id = Convert.ToInt32(itemDataList[i]["id"]);
                itemModel.Name = itemDataList[i]["name"].ToString();
                itemModel.Count = Convert.ToInt32(itemDataList[i]["count"]);
                if (type.Equals("lianQi_Q_Yes"))
                {
                    BaseConfig.lianQi_Q_Yes.Add(itemModel);
                }
                else if (type.Equals("lianQi_Z_Yes"))
                {
                    BaseConfig.lianQi_Z_Yes.Add(itemModel);
                }
                else if (type.Equals("lianQi_H_Yes"))
                {
                    BaseConfig.lianQi_H_Yes.Add(itemModel);
                }
                else if (type.Equals("zhuJi_Q_Yes"))
                {
                    BaseConfig.zhuJi_Q_Yes.Add(itemModel);
                }
                else if (type.Equals("zhuJi_Z_Yes"))
                {
                    BaseConfig.zhuJi_Z_Yes.Add(itemModel);
                }
                else if (type.Equals("zhuJi_H_Yes"))
                {
                    BaseConfig.zhuJi_H_Yes.Add(itemModel);
                }
                else if (type.Equals("jieJing_Q_Yes"))
                {
                    BaseConfig.jieJing_Q_Yes.Add(itemModel);
                }
                else if (type.Equals("jieJing_Z_Yes"))
                {
                    BaseConfig.jieJing_Z_Yes.Add(itemModel);
                }
                else if (type.Equals("jieJing_H_Yes"))
                {
                    BaseConfig.jieJing_H_Yes.Add(itemModel);
                }
                else if (type.Equals("jinDan_Q_Yes"))
                {
                    BaseConfig.jinDan_Q_Yes.Add(itemModel);
                }
                else if (type.Equals("jinDan_Z_Yes"))
                {
                    BaseConfig.jinDan_Z_Yes.Add(itemModel);
                }
                else if (type.Equals("jinDan_H_Yes"))
                {
                    BaseConfig.jinDan_H_Yes.Add(itemModel);
                }
                else if (type.Equals("juLing_Q_Yes"))
                {
                    BaseConfig.juLing_Q_Yes.Add(itemModel);
                }
                else if (type.Equals("juLing_Z_Yes"))
                {
                    BaseConfig.juLing_Z_Yes.Add(itemModel);
                }
                else if (type.Equals("juLing_H_Yes"))
                {
                    BaseConfig.juLing_H_Yes.Add(itemModel);
                }
                else if (type.Equals("yuanYing_Q_Yes"))
                {
                    BaseConfig.yuanYing_Q_Yes.Add(itemModel);
                }
                else if (type.Equals("yuanYing_Z_Yes"))
                {
                    BaseConfig.yuanYing_Z_Yes.Add(itemModel);
                }
                else if (type.Equals("yuanYing_H_Yes"))
                {
                    BaseConfig.yuanYing_H_Yes.Add(itemModel);
                }
                else if (type.Equals("huaShen_Q_Yes"))
                {
                    BaseConfig.huaShen_Q_Yes.Add(itemModel);
                }
                else if (type.Equals("huaShen_Z_Yes"))
                {
                    BaseConfig.huaShen_Z_Yes.Add(itemModel);
                }
                else if (type.Equals("huaShen_H_Yes"))
                {
                    BaseConfig.huaShen_H_Yes.Add(itemModel);
                }
                else if (type.Equals("wuDao_Q_Yes"))
                {
                    BaseConfig.wuDao_Q_Yes.Add(itemModel);
                }
                else if (type.Equals("wuDao_Z_Yes"))
                {
                    BaseConfig.wuDao_Z_Yes.Add(itemModel);
                }
                else if (type.Equals("wuDao_H_Yes"))
                {
                    BaseConfig.wuDao_H_Yes.Add(itemModel);
                }
                else if (type.Equals("yuHua_Q_Yes"))
                {
                    BaseConfig.yuHua_Q_Yes.Add(itemModel);
                }
                else if (type.Equals("yuHua_Z_Yes"))
                {
                    BaseConfig.yuHua_Z_Yes.Add(itemModel);
                }
                else if (type.Equals("yuHua_H_Yes"))
                {
                    BaseConfig.yuHua_H_Yes.Add(itemModel);
                }
                else if (type.Equals("dengXian_Q_Yes"))
                {
                    BaseConfig.dengXian_Q_Yes.Add(itemModel);
                }
                else if (type.Equals("dengXian_Z_Yes"))
                {
                    BaseConfig.dengXian_Z_Yes.Add(itemModel);
                }
                else if (type.Equals("dengXian_H_Yes"))
                {
                    BaseConfig.dengXian_H_Yes.Add(itemModel);
                }
            }
        }
        /// <summary>
        /// 添加道具
        /// </summary>
        /// <param name="type"></param>
        /// <param name="data"></param>
        public static void AddWelfares(string type, JObject data)
        {
            string itemData = data[type].ToString();
            JArray itemDataList = JArray.Parse(itemData);
            for (int i = 0; i < itemDataList.Count; i++)
            {
                ItemModel itemModel = new ItemModel();
                itemModel.Id = Convert.ToInt32(itemDataList[i]["id"]);
                itemModel.Name = itemDataList[i]["name"].ToString();
                itemModel.Count = Convert.ToInt32(itemDataList[i]["count"]);
                if (type.Equals("lianQi_Welfare"))
                {
                    BaseConfig.lianQi_Welfare.Add(itemModel);
                }
                else if (type.Equals("zhuJi_Welfare"))
                {
                    BaseConfig.zhuJi_Welfare.Add(itemModel);
                }
                else if (type.Equals("jieJing_Welfare"))
                {
                    BaseConfig.jieJing_Welfare.Add(itemModel);
                }
                else if (type.Equals("jinDan_Welfare"))
                {
                    BaseConfig.jinDan_Welfare.Add(itemModel);
                }
                else if (type.Equals("juLing_Welfare"))
                {
                    BaseConfig.juLing_Welfare.Add(itemModel);
                }
                else if (type.Equals("yuanYing_Welfare"))
                {
                    BaseConfig.yuanYing_Welfare.Add(itemModel);
                }
                else if (type.Equals("huaShen_Welfare"))
                {
                    BaseConfig.huaShen_Welfare.Add(itemModel);
                }
                else if (type.Equals("wuDao_Welfare"))
                {
                    BaseConfig.wuDao_Welfare.Add(itemModel);
                }
                else if (type.Equals("yuHua_Welfare"))
                {
                    BaseConfig.yuHua_Welfare.Add(itemModel);
                }
                else if (type.Equals("dengXian_Welfare"))
                {
                    BaseConfig.dengXian_Welfare.Add(itemModel);
                }

            }
        }

        /// <summary>
        /// 宗门发放突破物资区域配置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="data"></param>
        public static void GetAreaConfig(string name, string data)
        {
            JObject dataObject = JObject.Parse(data);
            if (name.Equals("daZhangLao"))
            {
                BaseConfig.school.IsDaZhangLao = Convert.ToBoolean(dataObject["enable"]);
                BaseConfig.school.DaZhangLaoArea = JsonConvert.DeserializeObject<List<AreaConfig>>(dataObject["areaConfig"].ToString());
                // LogUtil.Log("太阳球MOD", "大长老区域配置：" + JsonConvert.SerializeObject(BaseConfig.school.DaZhangLaoArea));
            }
            else if (name.Equals("zhangLao"))
            {
                BaseConfig.school.IsZhangLao = Convert.ToBoolean(dataObject["enable"]);
                BaseConfig.school.ZhangLaoArea = JsonConvert.DeserializeObject<List<AreaConfig>>(dataObject["areaConfig"].ToString());
            }
            else if (name.Equals("zhenChuan"))
            {
                BaseConfig.school.IsZhenChuan = Convert.ToBoolean(dataObject["enable"]);
                BaseConfig.school.ZhenChuanArea = JsonConvert.DeserializeObject<List<AreaConfig>>(dataObject["areaConfig"].ToString());
            }
            else if (name.Equals("neiMen"))
            {
                BaseConfig.school.IsNeiMen = Convert.ToBoolean(dataObject["enable"]);
                BaseConfig.school.NeiMenArea = JsonConvert.DeserializeObject<List<AreaConfig>>(dataObject["areaConfig"].ToString());
            }
            else if (name.Equals("waiMen"))
            {
                BaseConfig.school.IsWaiMen = Convert.ToBoolean(dataObject["enable"]);
                BaseConfig.school.WaiMenArea = JsonConvert.DeserializeObject<List<AreaConfig>>(dataObject["areaConfig"].ToString());
            }
        }
    }
}
